package com.example.huazong.myapplication.bottomnavigation;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;


import com.example.huazong.myapplication.R;
import com.example.huazong.myapplication.ThirdActivity;

import java.io.IOException;

/**
 * Created by huazong on 2021/6/7.
 */

public class MusicActivity extends Fragment {
    Intent intent;
    TextView txt_songname;
    ImageButton btn_Pre, btn_Play, btn_Next, btn_back;
    MediaPlayer mediaPlayer;
    String song_path;
    ListView MusicList;
    int mPosition;
    boolean isStop = true;
    String[] MusicName = {"七里香-周杰伦.mp3", "兰亭序-周杰伦.mp3", "反方向的钟-周杰伦.mp3", "告白气球-周杰伦.mp3", "我要夏天-周杰伦.mp3", "晴天-周杰伦.mp3", "本草纲目-周杰伦.mp3", "烟花易冷-周杰伦.mp3", "稻香-周杰伦.mp3", "红尘客栈-周杰伦.mp3", "虹-菅田将晖.mp3"};
    String[] MusicData = {"七里香-周杰伦.mp3", "兰亭序-周杰伦.mp3", "反方向的钟-周杰伦.mp3", "告白气球-周杰伦.mp3", "我要夏天-周杰伦.mp3", "晴天-周杰伦.mp3", "本草纲目-周杰伦.mp3", "烟花易冷-周杰伦.mp3", "稻香-周杰伦.mp3", "红尘客栈-周杰伦.mp3", "虹-菅田将晖.mp3"};


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.music, null);

        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //在布局中找到列表组件MusicList
        MusicList = (ListView) view.findViewById(R.id.MusicList);
        //三个音乐播放按钮
        btn_Pre = (ImageButton) view.findViewById(R.id.btn_Pre);
        btn_Play = (ImageButton) view.findViewById(R.id.btn_Play);
        btn_Next = (ImageButton) view.findViewById(R.id.btn_Next);
        //获取播放歌曲名字
        txt_songname = (TextView) view.findViewById(R.id.txt_songname);
        //返回按钮
        btn_back = (ImageButton) view.findViewById(R.id.btn_back);
        //设置列表的适配器
        ArrayAdapter<String> madapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, MusicData);
        MusicList.setAdapter(madapter);




        //播放暂停图片切换
        initView();



        //列表的点击事件
        MusicList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                String songName = MusicName[position];
                PlaySong(songName);
                txt_songname.setText(songName);
                mPosition = position;
                btn_Play.setImageResource(R.drawable.ic_pause_24dp);

            }
        });
        //切换上一首歌
        btn_Pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mPosition > 0) {
                    mPosition--;
                }
                String name = MusicName[mPosition];
                txt_songname.setText(name);
                PlaySong(MusicName[mPosition]);


            }
        });
        //切换下一首歌
        btn_Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mPosition < 10) {
                    mPosition++;
                }
                String name = MusicName[mPosition];
                txt_songname.setText(name);
                PlaySong(MusicName[mPosition]);


            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(getContext(), ThirdActivity.class);
                startActivity(intent);
            }
        });

    }

    //根据文件名播放音频文件
    public void PlaySong(String song_name) {
//        String sd_path = Environment.getExternalStorageDirectory().getAbsolutePath();//获取SD卡路径
        song_path = "/sdcard/" + "/Music/" + song_name;
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(song_path);
            mediaPlayer.prepare();
            mediaPlayer.start();
            Log.i("歌曲路径:", song_path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void initView() {
        mediaPlayer = new MediaPlayer();
        btn_Play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    isStop = true;
                    btn_Play.setImageResource(R.drawable.ic_play_24dp);
                } else {
                    mediaPlayer.start();
                    isStop = false;
                    btn_Play.setImageResource(R.drawable.ic_pause_24dp);
                }
            }
        });
    }




}



